//
//  UIColor.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/30.
//

import Foundation
import UIKit

extension UIColor {
    ///  便利构造函数  根据RGB快速生成颜色 取值 0～255.0
    convenience init(r:UInt32 ,g:UInt32 , b:UInt32 , a:CGFloat = 1.0) {
        self.init(red: CGFloat(r) / 255.0,
                  green: CGFloat(g) / 255.0,
                  blue: CGFloat(b) / 255.0,
                  alpha: a)
        
    }
    // MARK:- 把#ffffff颜色转为UIColor
    class func hex(hexString: String) -> UIColor {
        var cString: String = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
        if cString.count < 6 { return UIColor.black }
        
        let index = cString.index(cString.endIndex, offsetBy: -6)
        let subString = cString[index...]
        if cString.hasPrefix("0X") { cString = String(subString) }
        if cString.hasPrefix("#") { cString = String(subString) }
        
        if cString.count != 6 { return UIColor.black }
        
        var range: NSRange = NSMakeRange(0, 2)
        let rString = (cString as NSString).substring(with: range)
        range.location = 2
        let gString = (cString as NSString).substring(with: range)
        range.location = 4
        let bString = (cString as NSString).substring(with: range)
        
        var r: UInt32 = 0x0
        var g: UInt32 = 0x0
        var b: UInt32 = 0x0
        
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(r: r, g: g, b: b)
    }
    
    // MARK:- 把#ffffff颜色转为UIImage
    class func creatImageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    /// 随机生成颜色，包含纯白色与黑色。
    open class var random: UIColor {
           return UIColor(red: CGFloat(arc4random_uniform(256))/255.0,
                          green: CGFloat(arc4random_uniform(256))/255.0,
                          blue: CGFloat(arc4random_uniform(256))/255.0,
                          alpha: 1.0)
       }
    
    /// 随机生成颜色，远离白色与黑色。
    open class var randomColor: UIColor {
        let hue :CGFloat =  CGFloat(arc4random_uniform(256)) / 255.0 ;  //0.0 to 1.0
        let saturation :CGFloat = CGFloat(arc4random() % 128) / 255.0  + 0.5;  // 0.5 to 1.0,away from white
        let brightness :CGFloat = CGFloat(arc4random_uniform(128)) / 255.0 + 0.5;  //0.5 to 1.0,away from black
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
    }
    
    ///
    /// Conversion of hexadecimal encoding to RGB format
    ///
    /// - parameter hexString: Hexadecimal code
    /// - parameter alpha: CGFloat, default is 1.0
    ///
    /// - returns: Hexadecimal coded color
    ///
    class func sg_color(hexString: String, alpha: CGFloat = 1.0) -> UIColor {
        let tempHexString = hexString.replacingOccurrences(of: "#", with: "")
        let scanner = Scanner(string: tempHexString)
        var color: UInt64 = 0
        scanner.scanHexInt64(&color)
         
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
         
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
    
    
}
